package rdp;

import net.minecraft.server.v1_15_R1.NBTTagCompound;
import net.minecraft.server.v1_15_R1.NBTTagString;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;
import java.util.Map;

public class RDPUtil {
    public static void EXECUTE(String cmd) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
    }
    public static void EXECUTE(Player player, String cmd) {
        player.chat(cmd);
    }
    public static void EXECUTE(CommandSender sender, String cmd) {
        EXECUTE(cmd);
    }

    public static void NOTICE(String msg) {
        Bukkit.getServer().broadcastMessage(msg);
    }

    public static void SEND(Player player, String msg) {
        player.sendMessage(msg);
    }
    public static void SEND(Player player, String[] msg) {
        player.sendMessage(msg);
    }
    public static void SEND(CommandSender sender, String msg) {
        sender.sendMessage(msg);
    }
    public static void SEND(CommandSender sender, String[] msg) {
        sender.sendMessage(msg);
    }
    public static void SEND(String msg) {
        SEND(Bukkit.getConsoleSender(), msg);
    }
    public static void SEND(String[] msg) {
        SEND(Bukkit.getConsoleSender(), msg);
    }

    public static boolean CHECK(String param, String[] list) {
        for (String temp : list) if (param.equalsIgnoreCase(temp)) return true;
        return false;
    }
    public static void LOG(String type, String msg) {
        SEND(getColor(type) + "[RDP]§f" + msg);
    }
    private static String getColor(String type) {
        switch (type) {
            case "default"://white
                return "§f";
            case "warning"://yellow
                return "§e";
            case "error"://red
                return "§4";
            case "success"://green
                return "§2";
            case "info"://blue
                return "§b";
            default://blank
                return "";
        }
    }

    public static String FORMAT(String str) {
        String[] get = new String[]{"&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "&f"};
        for (String temp : get) {
            if (str.indexOf(temp) != -1) {
                str = str.replace(temp, temp.replace("&", "§"));
            }
        }
        return str;
    }

    public static ItemStack ITEM(Map map) {
        net.minecraft.server.v1_15_R1.ItemStack nms = CraftItemStack.asNMSCopy(new ItemStack((Material) map.get("MATERIAL")));
        NBTTagCompound cpd = (nms.hasTag()) ? nms.getTag() : new NBTTagCompound();
        cpd.set("Left", NBTTagString.a((String) map.get("LEFT")));
        cpd.set("Right", NBTTagString.a((String) map.get("RIGHT")));
        nms.setTag(cpd);
        ItemStack is = CraftItemStack.asBukkitCopy(nms);
        ItemMeta im = is.getItemMeta();
        im.setDisplayName((String) map.get("NAME"));
        im.setLore((List<String>) map.get("LORE"));
        is.setItemMeta(im);
        return is;
    }
}
